package com.hbmcc.deviceinfo.ui.main

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bun.miitmdid.core.ErrorCode
import com.bun.miitmdid.core.MdidSdkHelper
import com.bun.miitmdid.interfaces.IIdentifierListener
import com.bun.miitmdid.interfaces.IdSupplier
import com.hbmcc.deviceinfo.R
import com.hbmcc.deviceinfo.utils.DeviceUtil
import com.hbmcc.deviceinfo.weight.RecyclerViewDivider


const val TAG = "MainActivity"

class MainActivity : AppCompatActivity(), MainCallback {

    private lateinit var mainAdapter: MainAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val recyclerView: RecyclerView = findViewById(R.id.rcyList)
        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        recyclerView.layoutManager = layoutManager
        val divider = RecyclerViewDivider(
            this, LinearLayout.HORIZONTAL, 1,
            Color.parseColor("#ffe5e5e5")
        )
        val margin: Int = DeviceUtil.dp2px(12F, this)
        divider.setMarginHorizontal(margin, margin)
        recyclerView.addItemDecoration(divider)
        mainAdapter = MainAdapter(this)
        recyclerView.adapter = mainAdapter

        initList()
    }

    @SuppressLint("HardwareIds")
    private fun initList() {
        val list: MutableList<MainBean> = ArrayList()

        list.add(MainBean("设备类型(deviceType)", DeviceUtil.intDeviceType(this)))
        list.add(MainBean("客户端操作系统版本号(osVersion)", android.os.Build.VERSION.RELEASE))

        //通常被认为不可信，因为它有时为null。开发文档中说明了：这个ID会改变如果进行了出厂设置。并且，如果某个Andorid手机被Root过的话，这个ID也可以被任意改变。
        list.add(
            MainBean(
                "设备的 Android ID(androidId)",
                Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
            )
        )
        Log.e(
            TAG,
            "${
                Settings.Secure.getString(
                    contentResolver,
                    Settings.Secure.ANDROID_ID
                )
            } : ${
                Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
                    .toString().length
            }"
        )

        list.add(MainBean("android 设备的 imei(imei)", DeviceUtil.getImsi(this) ?: "null"))

        try{
            MdidSdkHelper.InitSdk(this, true, OAIDIIdentifierListener())
        }catch (e:Exception){
            list.add(MainBean("android 设备的 OAID(oaid)", "null"))
        }

        list.add(MainBean("屏幕密度(density)", "${resources.displayMetrics.scaledDensity}"))

        list.add(MainBean("运营商(carrier)", DeviceUtil.getNetworkOperatorName(this) ))

        list.add(MainBean("联网类型(networkType)", DeviceUtil.getNetworkType(this) ))
        list.add(MainBean("设备厂商(manufacturer)", "${android.os.Build.MANUFACTURER}" ))
        list.add(MainBean("设备型号(model)", "${android.os.Build.MODEL}" ))

        mainAdapter.updateListData(list)
    }




    inner class OAIDIIdentifierListener : IIdentifierListener {
        override fun OnSupport(isSupport: Boolean, supplier: IdSupplier?) {
            Log.e(TAG, "return value: $isSupport")
            if (isSupport && supplier != null) {
                mainAdapter.updateData(MainBean("android 设备的 OAID(oaid)", supplier.oaid ?: "null"))
            } else {
                mainAdapter.updateData(MainBean("android 设备的 OAID(oaid)", "null"))
            }
        }
    }

    override fun onItemClick(mainBean: MainBean, position: Int) {
    }
}